<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>

	<xsl:template match="/MenuDataResult/Item">
		<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
			<xsl:apply-templates select="Item"/>
		</siteMap>
	</xsl:template>

	<xsl:template match="Item">
		<siteMapNode>
			<xsl:attribute name="url">
				<xsl:call-template name="replace">
					<xsl:with-param name="str" select="ItemLink" />
					<xsl:with-param name="what">//summerwriters</xsl:with-param>
					<xsl:with-param name="by-what">/summerwriters</xsl:with-param>
				</xsl:call-template>
			</xsl:attribute>
			<xsl:attribute name="title">
				<xsl:value-of select="ItemTitle"/>
			</xsl:attribute>
			<xsl:attribute name="description">
				<xsl:value-of select="ItemTitle"/>
			</xsl:attribute>
		</siteMapNode>
	</xsl:template>

	<xsl:template match="Item[child::Menu]">
		<siteMapNode>
			<xsl:attribute name="url">
				<xsl:call-template name="replace">
					<xsl:with-param name="str" select="Menu/Link" />
					<xsl:with-param name="what">/WorkArea/linkit.aspx?LinkIdentifier=ID&amp;ItemID=</xsl:with-param>
					<xsl:with-param name="by-what">/summerwriters/subpage.aspx?id=</xsl:with-param>
				</xsl:call-template>
				<!--xsl:value-of select="Menu/Link"/-->
			</xsl:attribute>
			<xsl:attribute name="title">
				<xsl:value-of select="Menu/Title"/>
			</xsl:attribute>
			<xsl:attribute name="description">
				<xsl:value-of select="Menu/Title"/>
			</xsl:attribute>
			<xsl:apply-templates select="Menu/Item"/>
		</siteMapNode>
	</xsl:template>

	<xsl:template name="replace">
		<xsl:param name="str"/>
		<xsl:param name="what"/>
		<xsl:param name="by-what"/>
		<xsl:choose>
			<xsl:when test="not($str)"/>
			<xsl:when test="starts-with($str, $what)">
				<xsl:value-of select="$by-what"/>
				<xsl:call-template name="replace">
					<xsl:with-param name="str" select="substring($str, string-length($what)+1)"/>
					<xsl:with-param name="what" select="$what"/>
					<xsl:with-param name="by-what" select="$by-what"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="substring($str, 1, 1)"/>
				<xsl:call-template name="replace">
					<xsl:with-param name="str" select="substring($str, 2)"/>
					<xsl:with-param name="what" select="$what"/>
					<xsl:with-param name="by-what" select="$by-what"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>